/*
    
    Copyright (C) 2011  Antoine

 	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
   	the Free Software Foundation; either version 3 of the License, or
   	(at your option) any later version.

   	This program is distributed in the hope that it will be useful,
   	but WITHOUT ANY WARRANTY; without even the implied warranty of
   	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   	GNU General Public License for more details.

   	You should have received a copy of the GNU General Public License
   	along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */
package subs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.*;

public class ControllerSync implements ActionListener {

	JComboBox	combo;
	JTextArea	area;
	MainView	vue;
	Boolean		augmenter	= false;

	public ControllerSync(JComboBox combo_box, JTextArea text_area, MainView vue1) {
		combo = combo_box;
		area = text_area;
		vue = vue1;
	}

	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand().equals("Synchroniser")) {

			String text = "";

			int sync_duree = Integer.parseInt(vue.duree_area.getText());

			String action_string = (String) combo.getSelectedItem();
			if (action_string.equals("Augmenter")) {
				augmenter = true;
			}
			else {
				augmenter = false;
			}

			try {

				String[] temp; // Split de la string
				String delimiter = " --> "; // delimiter pour parser

				Calendar calendar_debut = Calendar.getInstance();
				Calendar calendar_fin = Calendar.getInstance();

				Date date_debut;
				Date date_fin;
				SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss,SSS");

				String line = null; // line boucle
				BufferedReader bufferReader;

				String str_timeline = "";
				boolean line_is_int = false;

				int k = 0;

				bufferReader = new BufferedReader(new StringReader(vue.editorPane.getText()));
				while ((line = bufferReader.readLine()) != null) {

					if (line_is_int) {

						try {
							temp = line.split(delimiter);
							date_debut = formatter.parse(temp[0]);
							calendar_debut.setTime(date_debut);

							date_fin = formatter.parse(temp[1]);
							calendar_fin.setTime(date_fin);
						}
						catch (Exception exception) {
							exception.printStackTrace();
						}

						if (augmenter) {
							calendar_debut.set(Calendar.SECOND, calendar_debut.get(Calendar.SECOND) + sync_duree);
							date_debut = calendar_debut.getTime();
							text += formatter.format(date_debut);
							vue.table.setValueAt(formatter.format(date_debut), k, 1);

							calendar_fin.set(Calendar.SECOND, calendar_fin.get(Calendar.SECOND) + sync_duree);
							date_fin = calendar_fin.getTime();
							text += " --> " + formatter.format(date_fin) + "\n";
							vue.table.setValueAt(formatter.format(date_fin), k, 2);
							k++;
						}
						else {
							calendar_debut.set(Calendar.SECOND, calendar_debut.get(Calendar.SECOND) - sync_duree);
							date_debut = calendar_debut.getTime();
							text += formatter.format(date_debut);
							vue.table.setValueAt(formatter.format(date_debut), k, 1);

							calendar_fin.set(Calendar.SECOND, calendar_fin.get(Calendar.SECOND) - sync_duree);
							date_fin = calendar_fin.getTime();
							text += " --> " + formatter.format(date_fin) + "\n";
							vue.table.setValueAt(formatter.format(date_fin), k, 2);
							k++;
						}

						str_timeline += line + "\n";
						line_is_int = false;
					}
					else {
						text += line + "\n";
					}

					char[] all = line.toCharArray();
					for (int i = 0; i < all.length; i++) {
						if (Character.isDigit(all[i])) {
							line_is_int = true;
						}
						else {
							line_is_int = false;
							break;
						}
					}

				}

				Lanceur.sub.updateEditor();
				Lanceur.sub.updatePane();
				vue.getJEditorPane().setText(text);
				vue.getJEditorPane().setCaretPosition(0);

			}
			catch (Exception ex) {

			}
		}

	}

}
